package com.warehouse.module.mp.api;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.warehouse.framework.common.util.object.BeanUtils;
import com.warehouse.module.mp.api.dto.MpUserDTO;
import com.warehouse.module.mp.dal.dataobject.user.MpUserDO;
import com.warehouse.module.mp.framework.mp.core.MpServiceFactory;
import com.warehouse.module.mp.service.user.MpUserService;
import com.warehouse.module.system.api.appuser.AppUserApi;
import com.warehouse.module.system.api.appuser.dto.AppUserDTO;
import me.chanjar.weixin.common.bean.WxOAuth2UserInfo;
import me.chanjar.weixin.common.bean.oauth2.WxOAuth2AccessToken;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.service.WxOAuth2Service;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.WxMpTemplateMsgService;
import me.chanjar.weixin.mp.bean.template.WxMpTemplate;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.util.List;

import static com.warehouse.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.warehouse.module.mp.enums.ErrorCodeConstants.USER_UPDATE_TAG_FAIL;

@Service
public class MpUserInfoServiceImpl implements MpUserInfoService{

    @Resource
    @Lazy // 延迟加载，解决循环依赖的问题
    private MpServiceFactory mpServiceFactory;

    @Resource
    private AppUserApi appUserApi;

    @Override
    public String getMpUserInfo(String appId, String code) {
        try {
            WxMpService mpService = mpServiceFactory.getMpService(appId);
            WxOAuth2Service oAuth2Service = mpService.getOAuth2Service();
            // 利用code获取accessToken
            WxOAuth2AccessToken accessToken = oAuth2Service.getAccessToken(code);
            return accessToken.getOpenId();
//            String openId = accessToken.getOpenId();

//            MpUserDO userDo = mpUserService.getUser(appId, openId);
//            AppUserDTO userDTO = appUserApi.getAppUserByAppIdOpenId(appId, openId);
//            if(userDTO == null){
//                userDTO = new AppUserDTO();
//                userDTO.setOpenid(openId);
//                userDTO.setAppId(appId);
//                userDTO = appUserApi.createAppUser(userDTO);
//            }
//            MpUserDTO mpUserDTO = BeanUtils.toBean(userDTO, MpUserDTO.class);
//            mpUserDTO.setUserId(userDTO.getId());
//            mpUserDTO.setOpenId(openId);
//            if(StringUtils.isNotEmpty(userDTO.getPhoneNumber())) {
//                mpUserDTO.setBindFlag(true);
//            }else{
//                mpUserDTO.setBindFlag(false);
//            }
//            return mpUserDTO;

        }catch (Exception ex){
            throw exception(USER_UPDATE_TAG_FAIL, ex.getMessage());
        }
    }

    public MpUserDTO getUserByOpenId(String appId, String openId){
        AppUserDTO userDTO = appUserApi.getAppUserByAppIdOpenId(appId, openId);
        MpUserDTO mpUserDTO = BeanUtils.toBean(userDTO, MpUserDTO.class);
        mpUserDTO.setUserId(userDTO.getId());
        mpUserDTO.setOpenId(openId);
        if(StringUtils.isNotEmpty(userDTO.getPhoneNumber())) {
            mpUserDTO.setBindFlag(true);
        }else{
            mpUserDTO.setBindFlag(false);
        }
        return mpUserDTO;
    }

    public List<WxMpTemplate> listWxTemplates(String appId) throws WxErrorException {
        WxMpService mpService = mpServiceFactory.getMpService(appId);
        WxMpTemplateMsgService templateMsgService = mpService.getTemplateMsgService();
        return templateMsgService.getAllPrivateTemplate();
    }

    public void addTemplateInfo(String appId){
        WxMpService mpService = mpServiceFactory.getMpService(appId);
        WxMpTemplateMsgService templateMsgService = mpService.getTemplateMsgService();
    }
}
